﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ERP.Manager.Core;
using ERP.Manager.DataEntity;
using ERP.Manager.DataProvider;
using ERP.Manager.DataHelper;
using System.Data.SqlClient;
using System.Data;

namespace ERP.Manager.SQLServerProvider
{
    public class ECS_attributeinfoRepository : IECS_attributeinfoRepository
    {
        #region 用商品类型编号查询商品类型的属性的方法
        public List<ECS_attributeinfo> all_ecs_attributeinfo_page(int cat_id, int pagecount)
        {
            List<ECS_attributeinfo> list = new List<ECS_attributeinfo>();
            SqlDataReader read = SQLHelper.ExecuteReader(CommandType.Text, ERPSQL.ALL_ECS_ATTRIBUTEINFO_PAGE, new SqlParameter("@cat_id", cat_id),
                new SqlParameter("@pagecount", pagecount));
            try
            {
                while (read.Read())
                {
                    ECS_attributeinfo attribute = new ECS_attributeinfo();
                    attribute.attr_id = int.Parse(read["attr_id"].ToString());
                    attribute.attr_name = read["attr_name"].ToString();
                    attribute.ecsgoodstypeinfo.cat_id = int.Parse(read["cat_id"].ToString());
                    attribute.ecsgoodstypeinfo.cat_name = read["cat_name"].ToString();
                    attribute.attr_input_type = int.Parse(read["attr_input_type"].ToString());
                    attribute.attr_values = read["attr_values"].ToString();
                    attribute.sort_order = int.Parse(read["sort_order"].ToString());
                    list.Add(attribute);
                }
                read.Close();
            }
            catch (Exception)
            {
                read.Close();
            }
            finally
            {
                read.Dispose();
            }
            return list;
        }
        #endregion

        #region 根据商品类型编号查询商品类型的属性的方法
        public List<ECS_attributeinfo> all_ecs_attributeinfo(int new_cat_id)
        {
            List<ECS_attributeinfo> list = new List<ECS_attributeinfo>();
            SqlDataReader read = SQLHelper.ExecuteReader(CommandType.Text, ERPSQL.ALL_ECS_ATTRIBUTEINFO, new SqlParameter("@new_cat_id", new_cat_id));
            try
            {
                while (read.Read())
                {
                    ECS_attributeinfo attribute = new ECS_attributeinfo();
                    attribute.attr_id = int.Parse(read["attr_id"].ToString());
                    attribute.attr_name = read["attr_name"].ToString();
                    attribute.ecsgoodstypeinfo.cat_id = int.Parse(read["cat_id"].ToString());
                    attribute.ecsgoodstypeinfo.cat_name = read["cat_name"].ToString();
                    attribute.attr_input_type = int.Parse(read["attr_input_type"].ToString());
                    attribute.attr_values = read["attr_values"].ToString();
                    attribute.sort_order = int.Parse(read["sort_order"].ToString());
                    list.Add(attribute);
                }
                read.Close();
            }
            catch (Exception)
            {
                read.Close();
            }
            finally
            {
                read.Dispose();
            }
            return list;
        }
        #endregion

        #region 添加商品类型属性的方法
        public int add_ecs_attrbuteinfo(ECS_attributeinfo attribute)
        {
            int rowcount = SQLHelper.ExecuteNonQuery(CommandType.Text, ERPSQL.ADD_ECS_ATTRIBUTEINFO,
                new SqlParameter("@cat_id", attribute.ecsgoodstypeinfo.cat_id),
                new SqlParameter("@attr_name", attribute.attr_name),
                new SqlParameter("@attr_input_type", attribute.attr_input_type),
                new SqlParameter("@attr_type", attribute.attr_type),
                new SqlParameter("@attr_values", attribute.attr_values),
                new SqlParameter("@attr_index", attribute.attr_index),
                new SqlParameter("@sort_order", attribute.sort_order),
                new SqlParameter("@is_linked", attribute.is_linked),
                new SqlParameter("@attr_group", attribute.attr_group));
            return rowcount;
        }
        #endregion

        #region 查询商品类型属性的详细信息
        public ECS_attributeinfo detail_ecs_attributeinfo(int attr_id)
        {
            ECS_attributeinfo attribute = new ECS_attributeinfo();
            SqlDataReader read = SQLHelper.ExecuteReader(CommandType.Text, ERPSQL.DETAIL_ECS_ATTRIBUTEINFO, new SqlParameter("@attr_id", attr_id));
            try
            {
                while (read.Read())
                {
                    attribute.attr_id = int.Parse(read["attr_id"].ToString());
                    attribute.attr_name = read["attr_name"].ToString();
                    attribute.ecsgoodstypeinfo.cat_id = int.Parse(read["cat_id"].ToString());
                    attribute.attr_input_type = int.Parse(read["attr_input_type"].ToString());
                    attribute.attr_type = int.Parse(read["attr_type"].ToString());
                    attribute.attr_values = read["attr_values"].ToString();
                    attribute.attr_index = int.Parse(read["attr_index"].ToString());
                    attribute.sort_order = int.Parse(read["sort_order"].ToString());
                    attribute.is_linked = int.Parse(read["is_linked"].ToString());
                    attribute.attr_group = int.Parse(read["attr_group"].ToString());
                }
                read.Close();
            }
            catch (Exception)
            {
                read.Close();
            }
            finally
            {
                read.Dispose();
            }
            return attribute;
        }
        #endregion

        #region 修改商品类型属性的方法
        public int update_ecs_attributeinfo(ECS_attributeinfo attribute)
        {
            int rowcount = SQLHelper.ExecuteNonQuery(CommandType.Text, ERPSQL.UPDATE_ECS_ATTRIBUTEINFO,
                new SqlParameter("@attr_id", attribute.attr_id),
                 new SqlParameter("@cat_id", attribute.ecsgoodstypeinfo.cat_id),
                new SqlParameter("@attr_name", attribute.attr_name),
                new SqlParameter("@attr_input_type", attribute.attr_input_type),
                new SqlParameter("@attr_type", attribute.attr_type),
                new SqlParameter("@attr_values", attribute.attr_values),
                new SqlParameter("@attr_index", attribute.attr_index),
                new SqlParameter("@is_linked", attribute.is_linked),
                new SqlParameter("@attr_group", attribute.attr_group));
            return rowcount;
        }
        #endregion

        #region 删除商品类型属性的方法
        public int delete_ecs_attributeinfo(int attr_id)
        {
            int rowcount = SQLHelper.ExecuteNonQuery(CommandType.Text, ERPSQL.DELETE_ECS_ATTRIBUTEINFO, new SqlParameter("@attr_id", attr_id));
            return rowcount;
        }
        #endregion

        #region 根据商品类型编号查询该商品类型的属性总数的方法
        public int all_ecs_attributeinfo_cat_id_count(int cat_id)
        {
            var rowcount = SQLHelper.ExecuteScalar(CommandType.Text, ERPSQL.ALL_ECS_ATTRIBUTEINFO_CAT_ID_COUNT,
                new SqlParameter("@cat_id", cat_id));
            return int.Parse(rowcount.ToString());
        }
        #endregion
    }
}
